<?php
$SITEROOT = '../';
include_once($SITEROOT . 'assets/snippets/shoppingCart/dbconfig.inc.php');
include_once($SITEROOT . 'assets/snippets/shoppingCart/kiwee.class.php');

$kiwee = new Kiwee();

if(IN_MANAGER_MODE!="true") die('<b>' . $_lang['kiwee_include_order_error'] . '</b>');
if ($_SESSION['mgrPermissions']['settings'] != "1") { echo 'Insufficient permissions for this module.'; exit; }

$theme = $manager_theme ? "$manager_theme/":"";
$modx->manager->initPageViewState();

// get and save search string
if($_REQUEST['op']=='reset') {
	$sqlQuery = $query = '';
	$_PAGE['vs']['search']='';
}
else {
	$sqlQuery = $query = isset($_REQUEST['search'])? $_REQUEST['search']:$_PAGE['vs']['search'];
	if(!is_numeric($sqlQuery)) $sqlQuery = mysql_escape_string($query);
	$_PAGE['vs']['search'] = $query;
}

// get & save listmode
$listmode = isset($_REQUEST['listmode']) ? $_REQUEST['listmode']:$_PAGE['vs']['lm'];
$_PAGE['vs']['lm'] = $listmode;
?>
<div id="overlay" onclick="hideBox()" style="display:none"></div>
	<div id="box" style="display:none">
	<img id="close" src="js/modal_window/images/close.gif" onclick="hideBox()" alt="Close" title="Close this Window" />
	<div id="boxtitle" style="color: #ff9933; font-weight: bold; font-size: 14px;">Item Changes</div>
	<div id="inner-box"></div>
</div>
<script type="text/javascript" src="media/script/scriptaculous/prototype.js"></script>
<script type="text/javascript" src="includes/dragdrophelper.js"></script>
<script type="text/javascript" src="media/script/scriptaculous/scriptaculous.js"></script>
<script type="text/javascript" src="media/script/scriptaculous/dragdrop.js"></script>
<script type="text/javascript" src="js/modal_window/javascripts/modal.js"></script>
<script type="text/javascript" src="actions/includes/interface.js"></script>
<script type="text/javascript" src="../assets/plugins/<?php if (file_exists('../assets/plugins/tinymce2111')){ echo 'tinymce2111'; } else { echo 'tinymce210'; }?>/jscripts/tiny_mce/tiny_mce.js"></script>
	
<link href="js/modal_window/stylesheets/modal.css" rel="stylesheet" type="text/css" />

<script language="javascript" type="text/javascript">
		tinyMCE.init({
			mode : "exact",
			elements: "Not_YET",
			theme : "advanced",
			plugins : "style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
			theme_advanced_buttons1_add_before : "newdocument,separator",
			theme_advanced_buttons1_add : "fontselect,fontsizeselect",
			theme_advanced_buttons2_add : "separator,insertdate,inserttime,preview,separator,forecolor,backcolor",
			theme_advanced_buttons2_add_before: "cut,copy,paste,pastetext,pasteword,separator,search,replace,separator",
			theme_advanced_buttons3_add_before : "tablecontrols,separator",
			theme_advanced_buttons3_add : "emotions,iespell,media,advhr,separator,print,separator,ltr,rtl,separator,fullscreen",
			theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,|,code",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_path_location : "bottom",
			content_css : "example_full.css",
			plugin_insertdate_dateFormat : "%Y-%m-%d",
			plugin_insertdate_timeFormat : "%H:%M:%S",
			extended_valid_elements : "hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
			external_link_list_url : "example_link_list.js",
			external_image_list_url : "example_image_list.js",
			flash_external_list_url : "example_flash_list.js",
			media_external_list_url : "example_media_list.js",
			template_external_list_url : "example_template_list.js",
			file_browser_callback : "fileBrowserCallBack",
			theme_advanced_resize_horizontal : false,
			theme_advanced_resizing : true,
			nonbreaking_force_tab : true,
			apply_source_formatting : true,
		});
	
    function createAllSortables() {
	        sections = ['chosenoptionsdiv','alloptionsdiv'];
	        Sortable.create('chosenoptionsdiv', {tag:'div',dropOnEmpty:true, containment:sections,only:'alloptdivs'});
	        Sortable.create('alloptionsdiv',{tag:'div',dropOnEmpty:true, containment:sections,only:'alloptdivs'});
	    }
	
			function enableEdit(cId){
			tinyMCE.execCommand('mceAddControl', true, cId);
		}
	
		function fileBrowserCallBack(field_name, url, type, win) {
			// This is where you insert your custom filebrowser logic
			// alert("Example of filebrowser callback: field_name: " + field_name + ", url: " + url + ", type: " + type);
	
			// Insert new URL, this would normaly be done in a popup
					win.document.forms[0].elements[field_name].value = "someurl.htm";
		}
	
		function updatecontenttemplate(){
			var updateconfirm = confirm("When you change the template, your unsaved changes will disappear, are you sure you want to update?");
			if (updateconfirm == true){
				id = document.getElementById('chunkid').value;
				var poststr = "trxntype=" + encodeURI('products_getchunk') + "&chunkid=" + encodeURI(id);
				makePOSTRequest('actions/ajax.php', poststr, 'products_getchunk');
			}
		}
							 
		function updateitemstats(){
			makePOSTRequest('actions/ajax.php', "trxntype=products_statsload", 'products_statsload');
		}
			
		function updateitemgrid(){
			var poststr="trxntype=" + encodeURI('products_gridload');
			makePOSTRequest('actions/ajax.php', poststr, 'products_gridload');
		}
		function products_updateoptions(id){
		   var order = Sortable.serialize('chosenoptionsdiv');
		   var poststr= "trxntype=" + encodeURI('products_updateoptions') +
						 "&itemid=" + encodeURI(id) +
						 "&" + order;
		   makePOSTRequest('actions/ajax.php', poststr, 'products_updateoptions');
		}
	
		function resetitemlist(){
			var poststr = "trxntype=" + encodeURI('products_search')
						+ "&sku=" + encodeURI('')
			makePOSTRequest('actions/ajax.php', poststr, 'products_search');
		}
		function products_options(id){
			var poststr = "trxntype=" + encodeURI('products_options')
						+ "&itemid=" + encodeURI(id);
			makePOSTRequest('actions/ajax.php', poststr, 'products_options');
		}
	   function products_filter(){
			var poststr = "trxntype=" + encodeURI('products_filter') + "&filterby=" + encodeURI(document.getElementById('filterbydropdown').value);
			makePOSTRequest('actions/ajax.php',poststr, 'products_filter');
		}
		function searchbysku(){
			var poststr = "trxntype=" + encodeURI('products_search')
						+ "&sku=" + encodeURI(document.getElementById('skutosearch').value)
			makePOSTRequest('actions/ajax.php', poststr, 'products_search');
		}
		function edititemproperties(id){
			var poststr = "trxntype=" + encodeURI('products_properties') +
						  "&itemid=" + encodeURI(id);
			makePOSTRequest('actions/ajax.php',poststr, 'products_properties');
		}
					
		function updateitemproperties(itemid){
				 var poststr = "trxntype=" + encodeURI('products_update')
							 + "&itemid=" + encodeURI(itemid)
							 + "&itemname=" + encodeURI(document.getElementById('edititemname').value)
							 + "&shortdescription=" + encodeURI(document.getElementById('editshortdescription').value)
						     + "&sku=" + encodeURI(document.getElementById('edititemsku').value)
							 + "&contentarea=" + encodeURI(document.getElementById('contentArea').value)
							 + "&price=" + encodeURI(document.getElementById('editprice').value)
							 + "&hazmat=" + encodeURI(document.getElementById('edithazmat').value)
							 + "&oversized=" + encodeURI(document.getElementById('editoversized').value)
							 + "&status=" + encodeURI(document.getElementById('editstatus').value)
							 + "&active=" + encodeURI(document.getElementById('editactive').checked)
							 + "&enablequote=" + encodeURI(document.getElementById('editenablequote').checked)
							 + "&template=" + encodeURI(document.getElementById('templateid').value)
							 + "&enableshop=" + encodeURI(document.getElementById('editenableshop').checked);
			makePOSTRequest('actions/ajax.php', poststr, 'products_update');
		}
	
	function ajaxrendercomponent(name) {
		if (name == 'products_options') {
			document.getElementById('inner-box').innerHTML=http_request.responseText;
			document.getElementById('boxtitle').innerHTML='Item Options';
			document.getElementById('box').style.width=415 + 'px'; 
			document.getElementById('box').style.left='2%'; 
			document.getElementById('box').style.height=460 + 'px'; 
			showBox();
			createAllSortables();
		} else if (name == 'products_updateoptions'){
			hideBox();
		} else if (name == 'products_search'){
			document.getElementById('itemmaintenancegrid').innerHTML=http_request.responseText;
		} else if (name == 'products_properties'){
				document.getElementById('inner-box').innerHTML=http_request.responseText;
				document.getElementById('boxtitle').innerHTML='Item Properties';
				showBox();
				document.getElementById('box').style.width='90%';
				document.getElementById('box').style.top='10%';
				document.getElementById('box').style.left='4%';
				document.getElementById('box').style.height='80%';
				enableEdit("contentArea");
		} else if (name == 'products_update'){
				hideBox();
				updateitemstats();
		} else if (name == 'products_statsload'){
			   document.getElementById('itemstats').innerHTML=http_request.responseText;
			   updateitemgrid();
		} else if (name == 'products_getchunk'){
				tinyMCE.execCommand( 'mceRemoveControl' , true, 'contentArea' );
				document.getElementById('contentArea').value=http_request.responseText;
				tinyMCE.execCommand( 'mceAddControl', true, 'contentArea' );
		} else if (name == 'products_gridload'){
			   document.getElementById('itemmaintenancegrid').innerHTML=http_request.responseText;
		} else {
			alert(name);
		}
	}
</script>
<!-- /TinyMCE -->
<link href="actions/includes/kiwee.css" rel="stylesheet" type="text/css" />
<div class="kiwee-msc" id="mascot"><a href="http://www.kiweecommerce.org"><img src="actions/includes/Kiwee-small.gif" /></a></div>		
<div class="subTitle">
<span class="right"></span>
</div>
<div class="sectionHeader"><?php echo $_lang["import_totals_header"]; ?></div>
<div class="sectionBody">
<div id="itemstats">
	<p><?php echo $_lang["import_totals_detail"]; ?></p>
<table border=0 cellspacing=0 cellpadding=0 align=center class="grid">
<tr class="gridHeader" align=center><td colspan=5><b>Products Summary</b></td></tr>
<tr class="sectionHeader"><td align="center"><b>New</b></td><td align="center"><b>Current</b></td><td align="center"><b>Deleted</b></td><td align="center"><b>Updated</b></td><td align="center"><b>Total</b></td></tr>
<?php
    $itemstat = $kiwee->getItemStats();
	echo '<tr><td align=center>' . $itemstat['newitems'] . '</td>';
	echo '<td align=center>' . $itemstat['currentitems'] . '</td>';
	echo '<td align=center>' . $itemstat['deleteditems'] . '</td>';
	echo '<td align=center>' . $itemstat['updateditems'] . '</td>';
	echo '<td align=center>' . $itemstat['totalitems'] . '</td></tr>';
?>
</table>
</div>
<button id="edititem" onclick="edititemproperties(0);">Add Item</button>
</div>
<div class="sectionHeader"><?php echo $_lang["item_import_header"]; ?></div>
<div class="sectionBody">
	<p><?php echo $_lang['item_import_desc']; ?></p>
	<div class="searchbar">
		<div><?php echo $_lang["item_searchby"]; ?><!--<?php echo $_lang["import_filterby"]; ?>--> </div>
		<div><input type="text" id="skutosearch" onChange="searchbysku();">
		<!--<select onchange="products_filter()" id="filterbydropdown"><option value="ALLITEMS">All Items</option><option value="NEWITEMS">New Items</option><option value="UPDATEDITEMS">Updated Items</option>
				 <option value="CURRITEMS">Current Items</option><option value="DELETEDITEMS">Deleted Items</option></select>--></div>
		<!--<div><a href="#" class="" title="<?php echo $_lang["search"];?>" onclick="searchResource();return false;"><?php echo $_lang["go"]; ?></a></div>-->
		<div><a href="#" class="" title="<?php echo $_lang['reset'];?>" onclick="resetitemlist();">
			<img src="media/style/<?php echo $manager_theme ? "$manager_theme/":""; ?>images/icons/refresh.gif" width="16" height="16"/></a></div>
		<!--<div><a href="#" class="" title="<?php echo $_lang["list_mode"];?>" onclick="changeListMode();return false;"><img src="media/style/<?php echo $manager_theme ? "$manager_theme/":""; ?>images/icons/table.gif" width="16" height="16"/></a></div>-->
		<!--<div><a style="float: right" href="index.php?a=116&cls=1"><img src="media/style/<?php echo $manager_theme ? "$manager_theme/":""; ?>images/icons/delete.gif"  align="absmiddle" /> <?php echo $_lang['clear_log']; ?></a></div>-->
	</div>
<div>
<br />
<div id="itemmaintenancegrid">
<?php
	$sql = "SELECT * from " . $kiwee->config['fpx'] . "items_audit";
	mysql_select_db($dbname);
	$ds = mysql_query($sql) or die ("MYSQL: " . mysql_error());
	include_once $base_path . "manager/includes/controls/datagrid.class.php";
	$grd = new DataGrid('',$ds, $number_of_results);
	$grd->noRecordMsg = $_lang["no_records_found"];
	$grd->cssClass="grid";
	$grd->columnHeaderClass="gridHeader";
	$grd->itemClass="gridItem";
	$grd->altItemClass="gridAltItem";
	$grd->fields="sku, status, name, description, disabled, amount";
	$grd->columns= $_lang["import_item_id"]. ", " . $_lang["import_status"] . "," . $_lang["import_name"] . ", " . $_lang["import_description"] . ", " . $_lang["import_isactive"] . ", " . $_lang["import_price"] . ", " . $_lang["import_edit_options"] . ", " . $_lang["import_addbutton"]; 
	$grd->colWidths="34,50,34,200,34,50,50,50";
	$grd->colAligns="left, left, left, left, center, left, center, center";
	$grd->colTypes=",,,,,currency,template:<button onclick=\"products_options([+items_id+])\">Edit Options</button> ,template:<button id=\"edititem\" onclick=\"edititemproperties([+id+]);\">Edit</button>";
	if($listmode=='1') $grd->pageSize=0;
	if($_REQUEST['op']=='reset') $grd->pageNumber = 1;
	echo $grd->render();
?>
</div>
</div>
	